from django.http import HttpResponse
# from django.shortcuts import render
# from django.views.generic import View
from django.views.generic.base import View

# import django decorator
from django.utils.decorators import method_decorator


# Create your views here.
def my_decorator(func):
    def wrapper(request, *args, **kwargs):
        print('自定义装饰器被调用了')
        print('请求路径：{}'.format(request.path))
        return func(request, *args, **kwargs)

    return wrapper


class BaseDecorator(object):
    @classmethod
    def as_view(cls, *args, **kwargs):
        view = super().as_view(*args, **kwargs)
        view = my_decorator(view)
        return view


# @method_decorator(my_decorator, name='dispatch')
class DemoView(BaseDecorator, View):
    # 全局请求添加装饰器
    # @method_decorator(my_decorator)
    # def dispatch(self, request, *args, **kwargs):
    #     return super().dispatch(request, *args, **kwargs)

    # 为特定请求添加装饰器
    # @method_decorator(my_decorator)
    def get(self, request):
        print("视图函数被调用")
        a = 1
        from .signals import oss_signal
        oss_signal.send(sender=a, books=a)
        return HttpResponse("get page")

    def post(self, request):
        return HttpResponse("post page")
